package com.atguigu.lease.web.admin.tasks;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyTask {
    @Autowired
   private LeaseAgreementService leaseAgreementService;
//修改房间租约状态
    //在每天的0.0。0秒执行一次
    //若租期时间小于当前时间
    //将房间状态为257的改为已到期
    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseAgreementStatus() {
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper =new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        lambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(lambdaUpdateWrapper);

    }
}
